package org.wanghong.org.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wanghong.basic.util.AjaxResult;
import org.wanghong.basic.util.PageList;
import org.wanghong.org.domain.Department;
import org.wanghong.org.query.DepartmentQuery;
import org.wanghong.org.service.IDepartmentService;

import java.util.List;

@Api(value = "部门API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改，否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {

            if (department.getId()==null)
                departmentService.add(department);
            else
                departmentService.update(department);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败！"+e.getMessage());
        }

    }

    //删除 delete
    @DeleteMapping("/{id}") // /department/1
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！"+e.getMessage());
        }
    }

    //获取一个 get
    @GetMapping("/{id}") //只有成功了才获取数据
    public AjaxResult getById(@PathVariable("id") Long id){

        try {
            Department department = departmentService.getById(id);
            return AjaxResult.me().setResult(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    //获取所有 get
    @GetMapping
    public AjaxResult getAll(){

        try {
            List<Department> list = departmentService.getAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }

    //分页+高级查询 post
    @PostMapping //只有成功了才获取数据
    public AjaxResult queryPage(@RequestBody DepartmentQuery query){

        try {
            PageList<Department> pageList = departmentService.queryPage(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @GetMapping("/tree")
    public AjaxResult loadTree(){

        try {
            List<Department> list = departmentService.getTree();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取部门树失败！"+e.getMessage());
        }
    }

}
